//
//  WLTableViewDelegate.m
//  列表视图代理封装
//
//  Created by shushui on 2018/8/16.
//  Copyright © 2018年 shushui. All rights reserved.
//

#import "WLTableViewDelegate.h"

@implementation WLTableViewDelegate
-(WLBaseTableViewLayerModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[WLBaseTableViewLayerModel alloc]init];
    }
    return _viewModel;
}
- (instancetype)initWithDelegate:(id)delegate
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
    }
    return self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.viewModel.viewLayer count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.viewModel.viewLayer objectAtIndex:section].item.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.delegate respondsToSelector:@selector(wl_tableView:layerModel:cellForRowAtIndexPath:)]){
        return [self.delegate wl_tableView:tableView layerModel:self.viewModel cellForRowAtIndexPath:indexPath];
    } else {
        NSString *cellT = [NSString stringWithFormat:@"CollectView-%ld-%ld",indexPath.section,indexPath.row];
        UITableViewCell  *otherCell = nil;
        WLBaseTableViewLayerRow *row = [self.viewModel getRowData:indexPath];
        if (row.viewClass) {
            [tableView registerClass:row.viewClass forCellReuseIdentifier:cellT];
            WLBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellT];
            cell.delegateTie = self.delegateTie;
            cell.rowData = row;
            otherCell = cell;
        } else if (row.nibClass){
            [tableView registerNib:[UINib nibWithNibName:NSStringFromClass(row.nibClass) bundle:nil] forCellReuseIdentifier:cellT];
            WLBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellT];
            cell.delegateTie = self.delegateTie;
            cell.rowData = row;
            otherCell = cell;
        } else {
            [tableView registerClass:UITableViewCell.class forCellReuseIdentifier:cellT];
            otherCell =  [tableView dequeueReusableCellWithIdentifier:cellT];
        }
        return otherCell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [[self.viewModel.viewLayer objectAtIndex:indexPath.section].item objectAtIndex:indexPath.row].cellHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if([self.delegate respondsToSelector:@selector(wl_tableView:layerModel:viewForHeaderInSection:)]){
        return [self.delegate wl_tableView:tableView layerModel:[self.viewModel.viewLayer objectAtIndex:section] viewForHeaderInSection:section];
    }
    return [[UIView alloc]init];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return [self.viewModel.viewLayer objectAtIndex:section].headerH;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if([self.delegate respondsToSelector:@selector(wl_tableView:layerModel:viewForFooterInSection:)]){
        return [self.delegate wl_tableView:tableView layerModel:[self.viewModel.viewLayer objectAtIndex:section] viewForFooterInSection:section];
    }
    return [[UIView alloc]init];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if ([self.viewModel.viewLayer objectAtIndex:section].footH){
        return [self.viewModel.viewLayer objectAtIndex:section].footH;
    }
    return 1;
}


#pragma mark -让 tableView 分割线连接起来-
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    //  NSNumber *height = @(cell.frame.size.height);
    if (![[self.viewModel.viewLayer objectAtIndex:indexPath.section].item objectAtIndex:indexPath.row].cellHeight) {
        [[self.viewModel.viewLayer objectAtIndex:indexPath.section].item objectAtIndex:indexPath.row].cellHeight = cell.frame.size.height;
        [self.viewModel.viewLayer objectAtIndex:indexPath.section].allCellH += cell.frame.size.height;
    }
    
    if([self.delegate respondsToSelector:@selector(wl_tableView:willDisplayCell:forRowAtIndexPath:)]){
        return [self.delegate wl_tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
    }
}
/** 在删除cell之后调用，停止显示cell的时候调用,界面不显示cell时。 */
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
    if([self.delegate respondsToSelector:@selector(wl_tableView:didEndDisplayingCell:forRowAtIndexPath:)]){
        return [self.delegate wl_tableView:tableView didEndDisplayingCell:cell forRowAtIndexPath:indexPath];
    }
}
//返回索引数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    if([self.delegate respondsToSelector:@selector(wl_sectionIndexTitlesForTableView:model:)]){
        return [self.delegate wl_sectionIndexTitlesForTableView:tableView model:self.viewModel];
    }
    return self.viewModel.sectionIndexTitles;
}

//响应点击索引时的委托方法    `此方法不写无影响`
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    if([self.delegate respondsToSelector:@selector(wl_tableView:sectionForSectionIndexTitle:atIndex:)]){
        return [self.delegate wl_tableView:tableView sectionForSectionIndexTitle:title atIndex:index];
    }
    return index;
}

// 结束拖拽时触发
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView  willDecelerate:(BOOL)decelerate{
    if([self.delegate respondsToSelector:@selector(wl_scrollViewDidEndDragging:willDecelerate:)]){
        [self.delegate wl_scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.deselectRow){
        [tableView deselectRowAtIndexPath:indexPath animated:self.viewModel.rowAtAnimated];
    }
    if([self.delegate respondsToSelector:@selector(wl_tableView:rowData:didSelectRowAtIndexPath:)]){
        [self.delegate wl_tableView:tableView rowData:self.viewModel.viewLayer[indexPath.section].item[indexPath.row] didSelectRowAtIndexPath:indexPath];
    }
}


#pragma mark -- WLEmptyDataSet
// 自定义显示视图
- (UIView *)customViewForWLEmptyDataSet:(UIScrollView *)scrollView{
    if([self.delegate respondsToSelector:@selector(wl_customViewForWLEmptyDataSet)]){
      return [self.delegate wl_customViewForWLEmptyDataSet];
    }
    return UIView.new;
}
@end
